package com.utils;

import java.util.HashMap;
import java.util.Map;

/**
 * 实体返回类
 */
public class Response {

    private Boolean success;

    private Integer code;

    private String message;

    private Map<String, Object> data = new HashMap<>();

    // 构造器私有
    private Response(){}

    public Response(Boolean success, Integer code, String message, Map<String, Object> data) {
        this.success = success;
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public Response(Boolean success, Integer code, String message) {
        this.success = success;
        this.code = code;
        this.message = message;
    }

    /**
     * 通用返回成功
     * @return
     */
    public static Response success() {
        return new Response(ResultCodeEnum.SUCCESS.getSuccess(),
                ResultCodeEnum.SUCCESS.getCode(),
                ResultCodeEnum.SUCCESS.getMessage());
    }

    /**
     * 通用返回失败
     * @return
     */
    public static Response error() {
        return new Response(ResultCodeEnum.UNKNOWN_ERROR.getSuccess(),
                ResultCodeEnum.UNKNOWN_ERROR.getCode(),
                ResultCodeEnum.UNKNOWN_ERROR.getMessage());
    }

    /**
     * 通用返回失败
     * @param message 自定义消息
     * @return
     */
    public static Response error(String message) {
        return new Response(ResultCodeEnum.UNKNOWN_ERROR.getSuccess(),
                ResultCodeEnum.UNKNOWN_ERROR.getCode(),
                message);
    }

    // 设置结果，形参为结果枚举
    public static Response setResult(ResultCodeEnum result) {
        return new Response(result.getSuccess(),
                result.getCode(),
                result.getMessage());
    }

    /**------------使用链式编程，返回类本身-----------**/

    // 自定义返回数据
    public Response data(Map<String,Object> map) {
        this.setData(map);
        return this;
    }

    // 通用设置data
    public Response data(String key, Object value) {
        this.data.put(key, value);
        return this;
    }

    // 自定义状态信息
    public Response message(String message) {
        this.setMessage(message);
        return this;
    }

    // 自定义状态码
    public Response code(Integer code) {
        this.setCode(code);
        return this;
    }

    // 自定义返回结果
    public Response success(Boolean success) {
        this.setSuccess(success);
        return this;
    }









    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Map<String, Object> getData() {
        return data;
    }

    public void setData(Map<String, Object> data) {
        this.data = data;
    }
}